
package com.yuke.cloud.service.cmc.config;

import com.yuke.cloud.common.config.YukeCoreConfig;
import com.yuke.cloud.common.core.config.PcObjectMapper;
import com.yuke.cloud.common.core.config.RedisConfiguration;
import com.yuke.cloud.common.core.config.SwaggerConfiguration;
import com.yuke.cloud.common.core.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * The class pmc web mvc config.
 *
 * @author
 */
@Configuration
@EnableWebMvc
@Import({SwaggerConfiguration.class, RedisConfiguration.class})
// mod by wg 20181220，增加redis配置类注入，采用统一的redis模板，否则可能导致redis无法读取到信息
@ComponentScan(basePackageClasses = {YukeCoreConfig.class})
public class CmcWebMvcConfig implements WebMvcConfigurer {

    @Resource
    private TokenInterceptor tokenInterceptor;

    @Value("${spring.profiles.active}")
    private String env;//当前激活的配置文件

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        if ("dev".equals(env)) { //开发环境对跨域进行配置
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*")
                    .allowCredentials(true);
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/");
    }

    /**
     *
     *
     * @param registry the registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/pay/alipayCallback", "/order/pay/notify", "/wxpay/notifyWeiXinPay",
                        "/bill/billpay/AliNotify", "/bill/billpay/WxNotify","/swagger-resources/**", "*.js",
                        "/**/*.js", "*.css", "/**/*.css", "*.html", "/**/*.html", "/mall/**");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        PcObjectMapper.buidMvcMessageConverter(converters);
    }

}
